package com.jy.rhin.application.cda;

import com.jy.api.hospital.HospitalAPI;
import com.jy.api.hospital.dto.HospitalDTO;
import com.jy.rhin.domain.cda.CDACreator;
import com.jy.rhin.infra.cda.CDAException;
import com.jy.rhin.infra.cda.chushengyxzm.ChuShengYXZMVo;
import com.jy.rhin.infra.cda.service.ChuShengYXZMService;
import com.jy.rhin.model.ChuShengYXZM;
import com.jy.rhin.model.ClinicalDocumentData;
import com.jy.rhin.model.CodeObject;
import com.jy.rhin.model.body.chushengyxzm.*;
import com.jy.rhin.model.body.xinshenger.ChuangJianZ;
import com.jy.rhin.model.header.Custodian;
import com.jy.rhin.model.header.Patient;
import com.jy.rhin.model.header.PatientRole;
import com.jy.rhin.utils.CDAUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class ChuShengYXZMApplication {

    @Resource
    private ChuShengYXZMService chuShengYXZMService;
    /*@Resource
    private DataMigrationAPI dataMigrationAPI;*/
    @Resource
    private CDACreator cdaCreator;

    @Resource
    private HospitalAPI hospitalAPI;

    public String create(String chuShengZMId,String fUWUJGBH) {
        return cdaCreator.create(createFS(chuShengZMId,fUWUJGBH), "02chushengyixuezm.ftl");
    }

    public ClinicalDocumentData createFS(String chuShengZMId,String fUWUJGBH) {
        ChuShengYXZMVo info = chuShengYXZMService.queryOne(fUWUJGBH, chuShengZMId);
        if(Objects.isNull(info)){
            throw new CDAException("主业务数据不存在",null);
        }

        HospitalDTO hospital = hospitalAPI.info(info.getFUWUJGBH()).getData();
//        HospitalDTO hospital = dataMigrationAPI.getByHospitalId(info.getFUWUJGBH()).getData();
        if(Objects.isNull(hospital)){
            throw new CDAException("机构数据不存在",null);
        }

        ChuShengYXZM docData = new ChuShengYXZM();
        setClinicalDocumentData(docData, info, hospital);
        docData.setQianFaRQ(CDAUtil.toD8(info.getQIANFARQ()));
        docData.setQianFaJGMC(hospital.getName());
        docData.setWenDangBH(info.getId()+"");
        docData.setChuShengXSR(createXinShengE(info));
        docData.setChuangJianZ(createChuangJianZ(info, hospital));
        docData.setZhuYaoJKWT(createJianKangWT(info));
        docData.setShengMingTZ(createShengMingTZ(info));
        docData.setMuQin(createMuQi(info));
        docData.setFuQin(createFuQi(info));
        docData.setJieShengXX(createJieShengXX(info));
        return docData;
    }

    private JieShengXX createJieShengXX(ChuShengYXZMVo info) {
        JieShengXX jieShengXX = new JieShengXX();
        //todo 缺失
        int chushengdlb = info.getCHUSHENGDLB();
        String chuShengDMC = "";
        switch (chushengdlb) {
            case 1:
                chuShengDMC = "医院";
                break;
            case 11:
                chuShengDMC = "省(市)级医院";
                break;
            case 12:
                chuShengDMC = "县(区)级医院";
                break;
            case 2:
                chuShengDMC = "妇幼保健院";
                break;
            case 3:
                chuShengDMC = "乡(街道)卫生机构";
                break;
            case 4:
                chuShengDMC = "村(诊所)卫生机构";
                break;
            case 5:
                chuShengDMC = "途中";
                break;
            case 6:
                chuShengDMC = "家庭(家中)";
                break;
            case 9:
                chuShengDMC = "其他";
                break;
        }
        jieShengXX.setChuShengD(new CodeObject(String.valueOf(chushengdlb),chuShengDMC));
        jieShengXX.setJieShengRXM(info.getJIESHENGRYXM());
        jieShengXX.setJieShengDWMC(info.getJIESHENGJGMC());
        return jieShengXX;
    }

    private FuMu createFuQi(ChuShengYXZMVo info) {
        FuMu muQi = new FuMu();
        muQi.setXingMing(info.getMUQINXM());
        muQi.setChuShengRQ(CDAUtil.toD8(info.getFUQINCSRQ()));
        muQi.setGuoJi(new CodeObject(info.getFUQINGJDM(), info.getFUQINGJMC()));
        muQi.setMinZu(new CodeObject(info.getFUQINMZDM(), info.getFUQINMZMC()));
        muQi.setShengFenZHM(info.getFUQINYXZJHM());
        return muQi;
    }

    private FuMu createMuQi(ChuShengYXZMVo info) {
        FuMu muQi = new FuMu();
        muQi.setXingMing(info.getMUQINXM());
        muQi.setChuShengRQ(CDAUtil.toD8(info.getMUQINCSRQ()));
        muQi.setGuoJi(new CodeObject(info.getMUQINGJDM(), info.getMUQINGJMC()));
        muQi.setMinZu(new CodeObject(info.getMUQINMZDM(), info.getMUQINMZMC()));
        muQi.setShengFenZHM(info.getMUQINYXZJHM());
        return muQi;
    }


    private ChuangJianZ createChuangJianZ(ChuShengYXZMVo info , HospitalDTO hospital) {
        ChuangJianZ chuangJianZ = new ChuangJianZ();
        chuangJianZ.setFangShiRQ(null != info.getQIANFARQ() ? CDAUtil.toD8(info.getQIANFARQ()) : "");
        chuangJianZ.setSuiFangYSXM(info.getQIANFARYXM());
        chuangJianZ.setHospitalId(info.getQIANFAJGDM());
        chuangJianZ.setHospitalName(info.getQIANFAJGMC());
        chuangJianZ.setHospitalAdress(hospital.getAddress());
        chuangJianZ.setChuanJianZId(info.getQIANFARYDM());
        return chuangJianZ;
    }

    private String shiJianBQ(Integer sj){
        if(sj<10){
            return "0"+sj;
        }else{
            return  sj+"";
        }
    }

    private ChuShengXSR createXinShengE(ChuShengYXZMVo info){
        ChuShengXSR data = new ChuShengXSR();
        data.setXingMing(info.getXINGMING());
        data.setXingBie(new CodeObject(shiFouYouZhi(info.getXINGBIE()),
                "0".equals(shiFouYouZhi(info.getXINGBIE()))? "未知的性别":
                        "1".equals(shiFouYouZhi(info.getXINGBIE()))? "男性":
                                "2".equals(shiFouYouZhi(info.getXINGBIE()))? "女性":
                                        "9".equals(shiFouYouZhi(info.getXINGBIE()))? "未说明的性别":""));
        data.setChuShengRQ(CDAUtil.toD8(info.getCHUSHENGRQ())+shiJianBQ(info.getCHUSHENGRQS())+shiJianBQ(info.getCHUSHENGRQF())+"00");
        data.setMenPaiHao(info.getCHUSHENGXXDZ());
        data.setJieDao(info.getCHUSHENGDZSQMC());
        data.setZhen(info.getCHUSHENGDZXZMC());
        List<ChuShengXSRJHR> jianHuRs = new ArrayList<>();
        ChuShengXSRJHR fuQing = new ChuShengXSRJHR();
        fuQing.setCodeObject(new CodeObject("51", "父亲"));
        fuQing.setMenPaiHao(shiFouYouZhi(info.getFUQINJZXXDZ()));
        fuQing.setJieDao(shiFouYouZhi(info.getFUQINJZDZSQMC()));
        fuQing.setZhen(shiFouYouZhi(info.getFUQINJZDZXZMC()));
        fuQing.setXian(shiFouYouZhi(info.getFUQINJZDZXMC()));
        fuQing.setShi(shiFouYouZhi(info.getFUQINJZDZSMC()));
        fuQing.setSheng(shiFouYouZhi(info.getFUQINJZDZSFMC()));
        fuQing.setJianHuRXM(shiFouYouZhi(info.getFUQINXM()));
        jianHuRs.add(fuQing);

        ChuShengXSRJHR MuQing = new ChuShengXSRJHR();
        MuQing.setCodeObject(new CodeObject("52", "母亲"));
        MuQing.setMenPaiHao(shiFouYouZhi(info.getMUQINJZXXDZ()));
        MuQing.setJieDao(shiFouYouZhi(info.getMUQINJZDZSQMC()));
        MuQing.setZhen(shiFouYouZhi(info.getMUQINJZDZXZMC()));
        MuQing.setXian(shiFouYouZhi(info.getMUQINJZDZXMC()));
        MuQing.setShi(shiFouYouZhi(info.getMUQINJZDZSMC()));
        MuQing.setSheng(shiFouYouZhi(info.getMUQINJZDZSFMC()));
        MuQing.setJianHuRXM(shiFouYouZhi(info.getMUQINMZMC()));
        jianHuRs.add(MuQing);
        data.setJianHuR(jianHuRs);

        data.setQu(info.getCHUSHENGDZXMC());
        data.setShi(info.getCHUSHENGDZSMC());
        data.setSheng(info.getCHUSHENGDZSFMC());
    	return data;
    }

    private String shiFouYouZhi(Object value) {
        if (!Objects.isNull(value)) {
            return String.valueOf(value);
        } else {
            return "-" ;
        }
    }
    
    private ZhuYaoJKWT createJianKangWT(ChuShengYXZMVo info){
        ZhuYaoJKWT data = new ZhuYaoJKWT();
    	data.setYunZhou(null != info.getCHUSHENGYZ()? String.valueOf(info.getCHUSHENGYZ()):"");
        String jiankangzq = info.getJIANKANGZQ();
        String jianKangZQMC = "";
        switch (jiankangzq) {
            case "1":
                jianKangZQMC = "良好";
                break;
            case "2":
                jianKangZQMC = "一般";
                break;
            case "3":
                jianKangZQMC = "差";
                break;
        }
        data.setJianKangZK(new CodeObject(String.valueOf(jiankangzq),jianKangZQMC));
    	return data;
    }

    
    private ShengMingTZ createShengMingTZ(ChuShengYXZMVo info){
        ShengMingTZ data = new ShengMingTZ();
        data.setChuShengSC(null != info.getSHENZHANG() ? info.getSHENZHANG().toString() : "");
        data.setChuShengTZ(null != info.getTIZHONG() ? String.valueOf(info.getTIZHONG()) : "");
        return data;
    }



    ClinicalDocumentData setClinicalDocumentData(ClinicalDocumentData clinicalDocumentData,ChuShengYXZMVo info, HospitalDTO hospital) {
        clinicalDocumentData.setEffectiveTime(CDAUtil.toD8(info.getQIANFARQ()));
        clinicalDocumentData.setCustodian(createCustodian(hospital));
        clinicalDocumentData.setPatientRole(createPatientRole(info));
        clinicalDocumentData.setVersionNumber("1.0");
        clinicalDocumentData.setId(""+info.getId());
        return clinicalDocumentData;
    }
    Custodian createCustodian(HospitalDTO hospital) {
        Custodian custodian = new Custodian();
        custodian.setId(hospital.getOutId());
        custodian.setName(hospital.getName());
        custodian.setAddr(hospital.getAddress());
        custodian.setTelecom(hospital.getDanWeiDH());
        return custodian;
    }
    private String hospitalIdCL(String hospitalId) {
        String subStr = "";
        if (hospitalId.length() < 10) {
            subStr = String.format("%-10s", subStr).replace(' ', '0'); // 不足10位则补0
        } else {
            subStr = hospitalId.substring(0, 10);// 截取前10位
        }
        return subStr;
    }

    PatientRole createPatientRole(ChuShengYXZMVo info) {
        PatientRole patientRole = new PatientRole();
        patientRole.setChuShengYXZMBH(info.getCHUSHENGZBH());
        patientRole.setId(CDAUtil.formatNumber(info.getGERENDABS()));
        patientRole.setPatient(createPatient(info));
        return patientRole;
    }


    Patient createPatient(ChuShengYXZMVo info) {
        Patient patient = new Patient();
        patient.setName(info.getXINGMING());
        patient.setId(info.getZHENGJIANHM());
        return patient;
    }
}
